package com.example.express_order.service;

import com.example.express_order.dao.OrderDao;
import com.example.express_order.domain.Order;
import com.example.express_order.domain.MyResponse;
import com.example.express_order.untils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderService {
    @Autowired
    OrderDao orderDao;

    public MyResponse addOrder(String userPhone, String expressDetailAddress,
                               String deliveryAddress, String deliveryMode, String expressType,
                               String estimatedPrice, String userLeisure, String stuffDetail){
        Order order = new Order();
        order.setOrderId(String.valueOf(UUID.randomUUID()));
        order.setUserPhone(userPhone);
        order.setExpressDetailAddress(expressDetailAddress);
        order.setDeliveryAddress(deliveryAddress);
        order.setDeliveryMode(deliveryMode);
        order.setExpressType(expressType);
        order.setEstimatedPrice(Float.parseFloat(estimatedPrice));
        order.setUserLeisure(userLeisure);
        order.setStuffDetail(stuffDetail);
        MyResponse myResponse;
        if (orderDao.addOrder(order)){
            myResponse = new MyResponse(200,true);

        }else {
            myResponse =new MyResponse(500,false);
        }
        return myResponse;
    }

    /**
     * 通过GPS和快递类型查询订单
     *
     */
    public List<Order> findOrdersByExpressDetailAddress(String expressDetailAddress,String expressType){
        if(expressType.equals("全部")){
            return orderDao.findOrdersByExpressDetailAddress(expressDetailAddress);
        }else {//通过位置和订单类型查询
            return orderDao.findOrderByExpressType(expressDetailAddress,expressType);
        }
    }

    /**
     * 搜索框搜索
     */
    public List<Order> findOrderBySearch(String searchContent,String param){
        List<Order> orderList;
        if (param.equals("快递地址搜索")){
            orderList=orderDao.findOrdersByExpressDetailAddress(searchContent);
        }else {
            orderList=orderDao.findOrdersByDeliveryAddress(searchContent);
        }
        return orderList;
    }



    /**
     * 接单，修改订单状态
     */
    public MyResponse acceptOrderByOrderIDAndCourierPhone(String courierPhone, String orderId){
        MyResponse myResponse;
        String nowDateTime = Utils.getNowDateTime("yyyy-MM-dd HH:mm:ss");
        if (orderDao.updateOrderStatusAndCourierByOrderID(1,courierPhone,orderId,nowDateTime)) {
            myResponse = new MyResponse(200,true);
        }else {
            myResponse = new MyResponse(500,false);
        }
        return myResponse;
    }

    /**
     * 客户订单完成
     */
    public MyResponse customerConfirmOrder(String orderId){
        MyResponse myResponse;
        String nowDateTime = Utils.getNowDateTime("yyyy-MM-dd HH:mm:ss");
        if (orderDao.modifyOrderStatusByOrderId(2,orderId,nowDateTime)) {
            myResponse = new MyResponse(200,true);
        }else {
            myResponse = new MyResponse(500,false);
        }
        return myResponse;
    }

    /**
     * 普通用户查看历史订单
     */
    public List<Order> findHistoryCommonUserOrderByPhone(String userPhone){
        return orderDao.findHistoryCommonUserOrderByPhone(userPhone);
    }


    /**
     * 快递员配送查看历史订单
     */
    public List<Order> findHistoryDeliveryOrderByCourierPhone(String courierPhone){
        return orderDao.findHistoryDeliveryOrderByCourierPhone(courierPhone);
    }

    /**
     * 已发布订单查看，订单审核
     */
    public List<Order> findOrderOfStatusEqualZero(){
        return orderDao.findOrderOfStatusEqualZero();
    }

    /**
     * 订单管理员管理订单
     */
    public MyResponse adminControlOrder(String orderId){
        MyResponse myResponse;
        //3代表订单审核未通过
        if (orderDao.adminCheckOrder(3,orderId)) {
            myResponse = new MyResponse(200,true);
        }else {
            myResponse = new MyResponse(500,false);
        }
        return myResponse;
    }

    public List<Order> findMyPostOrder(String userPhone){
        return orderDao.findMyPostOrder(userPhone);
    }
    public List<Order> findWaitFinishOrders(String userPhone){
        return orderDao.findWaitFinishOrders(userPhone);
    }
    public List<Order> findMyReceiveOrders(String courierPhone){
        return orderDao.findMyReceiveOrders(courierPhone);
    }



}
